package com.zealcomm.ivcscustomer.service

import android.app.*
import android.content.Intent
import android.graphics.BitmapFactory
import android.os.Binder
import android.os.Build
import android.os.IBinder
import com.zco.base.annotations.StreamType
import com.zco.ivcs.customer.IvcsCustomerCallback
import com.zealcomm.ivcscustomer.R
import com.zealcomm.ivcscustomer.app.IvcsApplication

class CaptureScreenService : Service() {

    override fun onBind(intent: Intent): IBinder {
        return Binder()
    }

    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        createNotificationChannel()
        intent?.getParcelableExtra<Intent>(CAPTURE_SCREEN_DATA)?.let {
            if (callback == null){
                callback = object : IvcsCustomerCallback{
                    override fun onSuccess() {
                    }

                    override fun onFailed(code: Int, error: String?) {
                    }
                }
            }
            IvcsApplication.screenShare(it, StreamType.CUSTOMER_SCREEN , callback!!)
        }
        return super.onStartCommand(intent, flags, startId)
    }

    override fun onDestroy() {
        stopForeground(true)
        callback = null
        super.onDestroy()
    }

    companion object {
        @JvmField
        var CAPTURE_SCREEN_DATA = "captureScreenData"
        @JvmField
        var CAPTURE_SCREEN_WIDTH = "captureScreenWidth"
        @JvmField
        var CAPTURE_SCREEN_HEIGHT = "captureScreenHeight"
        var callback: IvcsCustomerCallback? = object : IvcsCustomerCallback{
            override fun onSuccess() {
            }

            override fun onFailed(code: Int, error: String?) {
            }

        }
    }

    private fun createNotificationChannel() {
        val builder = Notification.Builder(this.applicationContext) //获取一个Notification构造器
        val nfIntent = Intent(this, MainActivity::class.java) //点击后跳转的界面，可以设置跳转数据
        val pendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.getActivity(this, 0, nfIntent, PendingIntent.FLAG_IMMUTABLE);
        } else {
            PendingIntent.getActivity(this, 0, nfIntent, PendingIntent.FLAG_ONE_SHOT);
            // PendingIntent.getActivity(this, 0, nfIntent, 0)
        }
        builder.setContentIntent(pendingIntent) // 设置PendingIntent
            .setLargeIcon(
                BitmapFactory.decodeResource(
                    this.resources,
                    R.mipmap.ic_launcher
                )
            ) // 设置下拉列表中的图标(大图标)
            //.setContentTitle("SMI InstantView") // 设置下拉列表里的标题
            .setSmallIcon(R.mipmap.ic_launcher) // 设置状态栏内的小图标
            .setContentText("屏幕共享中......") // 设置上下文内容
            .setWhen(System.currentTimeMillis()) // 设置该通知发生的时间

        /*以下是对Android 8.0的适配*/
        //普通notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            builder.setChannelId(this.javaClass.simpleName)
        }
        //前台服务notification适配
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            val channel = NotificationChannel(
                this.javaClass.simpleName,
                this.javaClass.simpleName,
                NotificationManager.IMPORTANCE_LOW
            )
            notificationManager.createNotificationChannel(channel)
        }

        val notification = builder.build() // 获取构建好的Notification
        notification.defaults = Notification.DEFAULT_SOUND //设置为默认的声音
        startForeground(110, notification)
    }
}